import Opt from '@/components/dt/info/Opt.js'
import billMgr from '@/biz/bonus/bill'

class AgencyColumn extends Opt {

  constructor(name, bonusId, update) {
    super()
    this.name = name
    this.bonusId = bonusId
    this.update = update
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [{
        type: 'index',
        title: '序号',
        minWidth: 50,
      },
      {
        field: 'adjustType',
        title: '调整类型',
        minWidth: 100,
        cell: {
          text: {
            type: 'dict',
            key: 'commissionAdjustType'
          }
        }
      },
      {
        field: 'reason',
        title: '调整原因',
        minWidth: 250,
      },
      {
        field: 'adjustAmount',
        title: '调整金额(元)',
        minWidth: 120,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        title: '是否计入奖金结算',
        minWidth: 130,
        cell: (info) => {
          if (this.name == 'billEdit') {
            return {
              edit: {
                key: 'isParticipate',
                switch: {},
                change: () => {
                  billMgr.agencyEdit(this.bonusId, info.adjustId, info.isParticipate).then(res => {
                    this.update()
                  }).catch(err => {
                    if (err.code != 5001) {
                      info.isParticipate = !info.isParticipate
                    }
                  })
                }
              },
            }
          } else {
            return {
              value: (v) => {
                return info.isParticipate ? '计入奖金结算' : '不计入奖金结算'
              },
              text: {
                color: info.isParticipate ? null : 'red'
              }
            }
          }
        }
      }
    ]
  }
}

export default AgencyColumn
